package ysq.xyg.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.extend.ehcache.EhCacheService;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.shop.dao.ShopItemCatDao;
import ysq.xyg.shop.model.po.ShopItemCat;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.shop.model.vo.ShopItemCatVo;
import ysq.xyg.shop.service.ShopItemCatService;
import ysq.xyg.web.model.po.WebArticleType;
import ysq.xyg.web.model.vo.WebArticleTypeVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 商城模块-商品分类service实现
 *
 * @author congcong tao
 * @date 2019-4-28 11:17:27
 */
@Service("shopItemCatService")
public class ShopItemCatServiceImpl extends BaseServiceImpl<ShopItemCat> implements ShopItemCatService {

    @Autowired
    private EhCacheService ehCacheService;
    @Resource
    private ShopItemCatDao shopItemCatDao;

    private ShopItemCat shopItemCat;

    @Autowired
    public void setBaseDao(ShopItemCatDao shopItemCatDao) {
        this.baseDao = shopItemCatDao;
    }

    /**
     * 分页查询
     *
     * @param shopItemCatVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:17:27
     */
    @Override
    public ReturnEntity page(ShopItemCatVo shopItemCatVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<ShopItemCat> list = acceptList(shopItemCatVo);
        PageInfo<ShopItemCat> page=new PageInfo<ShopItemCat>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param shopItemCatVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:17:27
     */
    @Override
    public ReturnEntity addOrUpdate(ShopItemCatVo shopItemCatVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(shopItemCatVo.getId())){
            shopItemCat=new ShopItemCat(currentUser);
            isUpate=false;
        }else{
            shopItemCat=selectByPrimaryKey(shopItemCatVo.getId());
            if(ObjectUtil.isNull(shopItemCat)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            shopItemCat.setUpdateTime(new Date());
            shopItemCat.setUpdateUser(currentUser.getId());
        }
        shopItemCat.setName(shopItemCatVo.getName());
        shopItemCat.setImage(shopItemCatVo.getImage());
        shopItemCat.setDescription(shopItemCatVo.getDescription());
        shopItemCat.setOrderNum(shopItemCatVo.getOrderNum());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(shopItemCat);
        }else{
            num=insert(shopItemCat);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        ehCacheService.evictShopItemCat();
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:17:27
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        ShopItemCat shopItemCat=selectByPrimaryKey(id);
        if(shopItemCat!=null){
            shopItemCat.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            shopItemCat.setUpdateTime(new Date());
            shopItemCat.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(shopItemCat);
            if(num>0){
                ehCacheService.evictShopItemCat();
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param shopItemCatVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-4-28 11:17:27
     */
    @Override
    public List<ShopItemCat> acceptList(ShopItemCatVo shopItemCatVo){
        Example example = new Example(ShopItemCat.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(shopItemCatVo.getName())){
            criteria.andLike("name","%" + shopItemCatVo.getName() + "%");
        }
        if(StringUtil.isNotEmpty(shopItemCatVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",shopItemCatVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return shopItemCatDao.selectByExample(example);
    }

    @Override
    public ReturnEntity acceptSelect(String code) {
        List<ShopItemCat> list = acceptList(new ShopItemCatVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(ShopItemCat entity : list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(entity.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
}
